home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Softshoe / Lisa's Portable Parts / Data / Data.h < prev   
Encoding:
Text File  |  2000-06-23  |  3.1 KB  |  113 lines

  1. // Data.h
  2.  
  3. #ifndef Data_h
  4. #define Data_h
  5.  
  6. #ifndef Integers_h
  7. #include "Integers.h"
  8. #endif
  9. #ifndef DebugMessage_h
  10. #include "DebugMessage.h"
  11. #endif
  12. #ifndef Assert_h
  13. #include "Assert.h"
  14. #endif
  15. #ifndef Range_h
  16. #include "Range.h"
  17. #endif
  18. #ifndef ConstArrayOf_h
  19. #include "ConstArrayOf.h"
  20. #endif
  21. #ifndef ConstData_h
  22. #include "ConstData.h"
  23. #endif
  24. #ifndef ArrayOf_h
  25. #include "ArrayOf.h"
  26. #endif
  27.  
  28. // The includes above have been flattened so that weak compilers
  29. // can cope better.
  30.  
  31. /*
  32.    A Data is a range of bytes, starting at Start() and
  33.    not including End().
  34. */
  35.  
  36. class Data
  37.   {
  38.     private:
  39.         uint8 *start;
  40.         uint8 *end;
  41.     
  42.     public:
  43.         Data()        : start(0), end(0)    {}
  44.  
  45.         Data( void *theStart, void *theEnd )
  46.           : start( static_cast<uint8 *>( theStart ) ),
  47.              end( static_cast<uint8 *>( theEnd ) )
  48.           {
  49.             Assert( theEnd >= theStart );
  50.             Assert( theStart != 0 || theEnd == 0 );
  51.           }
  52.         
  53.         Data( void *theStart, uint32 theLength )
  54.           : start( static_cast<uint8 *>( theStart ) ),
  55.              end( static_cast<uint8 *>( theStart ) + theLength )
  56.           {
  57.             Assert( theStart != 0 || theLength == 0 );
  58.           }
  59.  
  60.         Data( ArrayOf<uint8> array )
  61.           : start( array.Start() ),
  62.              end( array.End() )
  63.           {}
  64.  
  65.         uint8 *Start() const                                        { return start; }
  66.         uint8 *End() const                                        { return end; }
  67.  
  68.         uint32 Length() const                                    { return uint32( end - start ); }
  69.                 
  70.         uint32 BoundedLength( uint32 bound ) const        { return ( Length() <= bound ) ? Length() : bound; }
  71.  
  72.         bool IsEmpty() const                                        { return start >= end; }
  73.         bool Null() const                                            { return start == 0; }
  74.         
  75.         inline uint8& operator[]( uint32 i ) const;
  76.         
  77.         const Data Head( uint32 position ) const            { Assert( position <= Length() ); return Data( start, start+position ); }
  78.         const Data Tail( uint32 position ) const            { Assert( position <= Length() ); return Data( start+position, end ); }
  79.         const Data Middle( uint32 s, uint32 e ) const    { Assert( s <= e );  Assert( e <= Length() ); return Data( start+s, start+e ); }
  80.         const Data Middle( URange32 r ) const                { Assert( r.End() <= Length() );  return Data( start+r.Start(), start+r.End() ); }
  81.         
  82.         void Truncate( uint32 position )                        { Assert( position <= Length() ); end = start + position; }
  83.         void LimitLength( uint32 limit )                        { if ( limit < Length() )  end = start + limit; }
  84.         
  85.         void Shorten( uint32 amount )                            { Assert( amount <= Length() ); start += amount; }
  86.         void Lengthen( uint32 amount )                        { end += amount; }
  87.         
  88.         operator ConstData() const                                { return ConstData( start, end ); }
  89.         
  90.         bool StartsWith( ConstData d ) const                { return ConstData( start, end ).StartsWith( d ); }
  91.         bool EndsWith( ConstData d ) const                    { return ConstData( start, end ).EndsWith( d ); }
  92.         bool Contains( ConstData d ) const                    { return ConstData( start, end ).Contains( d ); }
  93.         
  94.         bool Contains( const void *p ) const                { return start <= p && p < end; }
  95.         
  96.         const Data operator<<( ConstData r ) const;        // returns unused space, so chaining works
  97.   };
  98.  
  99. class DataOverflow {};
  100.         
  101. inline uint8& Data::operator[]( uint32 i ) const
  102.   {
  103.     if ( i >= Length() )
  104.       {
  105.         Assert( 0 );
  106.         throw DataOverflow();
  107.       }
  108.     
  109.     return start[i];
  110.   }
  111.  
  112. #endif
  113.